<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>专利管理系统 - 登录</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Tailwind配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#0A2463',
                        accent: '#3E92CC',
                        neutral: '#F5F7FA',
                        'neutral-dark': '#1E293B'
                    },
                    fontFamily: {
                        inter: ['Inter', 'sans-serif'],
                    },
                }
            }
        }
    </script>

    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .login-shadow {
                box-shadow: 0 10px 25px -5px rgba(22, 93, 255, 0.1), 0 8px 10px -6px rgba(22, 93, 255, 0.1);
            }
            .bg-gradient-login {
                background: linear-gradient(135deg, #165DFF 0%, #0A2463 100%);
            }
        }
    </style>
</head>
<body class="font-inter bg-neutral min-h-screen flex items-center justify-center p-4">
<!-- 背景装饰 -->
<div class="fixed inset-0 bg-gradient-to-br from-primary/5 to-secondary/5 -z-10"></div>

<div class="w-full max-w-md">
    <!-- 登录卡片 -->
    <div class="bg-white rounded-2xl shadow-xl overflow-hidden login-shadow transition-all duration-300 hover:shadow-2xl">
        <!-- 卡片顶部 -->
        <div class="bg-gradient-login p-8 text-white text-center">
            <h2 class="text-2xl font-bold mb-2">专利管理系统</h2>
            <p class="text-white/80">请登录您的账户</p>
        </div>

        <!-- 登录表单 -->
        <div class="p-8">
            <form id="loginForm" action="#" th:action="@{/api/auth/login}" th:method="post" class="space-y-6">
                <!-- CSRF令牌 -->
                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">

                <!-- 用户名/邮箱 -->
                <div class="space-y-2">
                    <label for="username" class="block text-sm font-medium text-gray-700">用户名或邮箱</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-user text-gray-400"></i>
                        </div>
                        <input type="text" id="username" name="username" class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/30 transition-all duration-200" placeholder="请输入用户名或邮箱" required>
                    </div>
                </div>

                <!-- 密码 -->
                <div class="space-y-2">
                    <label for="password" class="block text-sm font-medium text-gray-700">密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-gray-400"></i>
                        </div>
                        <input type="password" id="password" name="password" class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm focus:border-primary focus:ring focus:ring-primary/30 transition-all duration-200" placeholder="请输入密码" required>
                        <button type="button" id="togglePassword" class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600">
                            <i class="fa fa-eye-slash"></i>
                        </button>
                    </div>
                </div>

                <!-- 记住我 -->
                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <input id="remember-me" name="remember-me" type="checkbox" class="h-4 w-4 text-primary focus:ring-primary/30 border-gray-300 rounded">
                        <label for="remember-me" class="ml-2 block text-sm text-gray-700">记住我</label>
                    </div>
                    <div class="text-sm">
                        <a href="#" class="font-medium text-primary hover:text-primary/80 transition-colors">忘记密码?</a>
                    </div>
                </div>

                <!-- 登录按钮 -->
                <div>
                    <button type="submit" class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg shadow-md hover:shadow-lg transition-all duration-200 transform hover:-translate-y-0.5" onclick="login()">
                        <i class="fa fa-sign-in mr-2"></i>登录
                    </button>
                </div>

                <!-- 分隔线 -->
                <div class="relative">
                    <div class="absolute inset-0 flex items-center">
                        <div class="w-full border-t border-gray-200"></div>
                    </div>
                    <div class="relative flex justify-center text-sm">
                        <span class="px-2 bg-white text-gray-500">或者</span>
                    </div>
                </div>

                <!-- 注册链接 -->
                <div class="text-center">
                    <p class="text-sm text-gray-600">
                        还没有账户? <a href="#" class="font-medium text-primary hover:text-primary/80 transition-colors">立即注册</a>
                    </p>
                </div>
            </form>

            <!-- 错误消息 - 已隐藏 -->
            <!--
            <div th:if="${param.error}" class="mt-6 p-4 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700">
                <i class="fa fa-exclamation-circle mr-2"></i> 用户名或密码错误
            </div>
            <div th:if="${param.logout}" class="mt-6 p-4 bg-green-50 border border-green-200 rounded-lg text-sm text-green-700">
                <i class="fa fa-check-circle mr-2"></i> 您已成功退出登录
            </div>
            -->
        </div>
    </div>

    <!-- 页脚 -->
    <div class="mt-6 text-center text-sm text-gray-500">
        <p>© 2023 专利管理系统. 保留所有权利.</p>
    </div>
</div>

<!-- JavaScript -->
<!-- jquery -->
<script src="/js/jquery-3.6.0.min.js"></script>
<script>
    // 切换密码可见性
    const togglePassword = document.getElementById('togglePassword');
    const passwordInput = document.getElementById('password');

    togglePassword.addEventListener('click', function() {
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);

        // 切换图标
        this.querySelector('i').classList.toggle('fa-eye');
        this.querySelector('i').classList.toggle('fa-eye-slash');
    });

    // 表单提交动画
    const loginForm = document.getElementById('loginForm');
    loginForm.addEventListener('submit', function(e) {
        // 显示加载状态
        const submitButton = this.querySelector('button[type="submit"]');
        submitButton.disabled = true;
        submitButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i>登录中...';
    });
</script>
<script>
    // 替换原来的login()函数
    function login(event) {
        event.preventDefault(); // 阻止表单默认提交

        const username = $('#username').val();
        const password = $('#password').val();
        const rememberMe = $('#remember-me').is(':checked');

        // 验证
        if (username.length < 2 || username.length > 20) {
            layer.msg("账号长度为2~20个字符");
            return;
        }
        if (password.length < 4 || password.length > 15) {
            layer.msg("密码长度为4~15个字符");
            return;
        }

        // 添加加载状态
        const submitButton = $('#loginForm button[type="submit"]');
        submitButton.prop('disabled', true);
        submitButton.html('<i class="fa fa-spinner fa-spin mr-2"></i>登录中...');

        // 获取CSRF令牌
        const csrfToken = $("input[name='" + $("meta[name='_csrf_parameter']").attr("content") + "']").val();

        $.ajax({
            url: "/api/auth/login",
            type: "POST",
            headers: {
                "X-CSRF-TOKEN": csrfToken // 手动添加CSRF令牌
            },
            data: JSON.stringify({
                username: username,
                password: password,
                rememberMe: rememberMe
            }),
            contentType: "application/json",
            success: function(data) {
                if (data.code === 200) {
                    window.location.href = "/admin-dashboard.html";
                } else {
                    layer.msg(data.msg || "登录失败");
                    submitButton.prop('disabled', false).html('<i class="fa fa-sign-in mr-2"></i>登录');
                }
            },
            error: function() {
                layer.msg("服务器错误");
                submitButton.prop('disabled', false).html('<i class="fa fa-sign-in mr-2"></i>登录');
            }
        });
    }

    // 绑定表单提交事件
    $('#loginForm').on('submit', login);


</script>

</body>
</html>